/**
 * 
 */
package com.tv.crawler;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import java.util.Map.Entry;

//import com.sun.mail.imap.protocol.Item;
import com.tv.data.TVProcessor;
import com.tv.htmlparser.TVJsoupParser;
import com.tv.tools.Configuration;

/**
 * @author Issac
 *
 */
public class MovieCrawler {
	
	private Map<String, String> tvList = null;
	
	/* 使用种子 url 初始化 URL 队列*/
	private void initCrawlerWithSeeds(String[] seeds)
	{
		for(int i=0;i<seeds.length;i++)
			LinkDB.addUnvisitedUrl(seeds[i]);
	}
	
	public MovieCrawler(){
		
	}

	public void movieCrawler(String[] seeds) {
		LinkFilter filter = new LinkFilter(){
			//提取以 http://www.twt.edu.cn 开头的链接
			public boolean accept(String url) {
				if(url.contains("http://theater.mtime.com/China_Guangdong_Province_Shenzen"))
					return true;
				else
					return false;
			}
		};
		
		//初始化tv队列
		//initUnVisitedTVUrl();

		
		//初始化 URL 队列
		initCrawlerWithSeeds(seeds);
		//循环条件：待抓取的链接不空且抓取的网页不多于 1000
		int i = 1;
		
		while(!LinkDB.unVisitedUrlsEmpty()&&LinkDB.getVisitedUrlNum()<=100)
		//while(!LinkDB.unVisitedUrlsEmpty() && i < tvList.size())
		{
			//队头 URL 出对
			String visitUrl=LinkDB.unVisitedUrlDeQueue();
			if(visitUrl==null){
				continue;
			}
			System.out.println(visitUrl);
			
			/*
			 * 封装相对路径
			 */
			/*if(!visitUrl.startsWith("http://v.163.com/tvlist/")){
				visitUrl = "http://v.163.com/tvlist/" + visitUrl;
			}*/
			
			MovieFileDownLoader downLoader=new MovieFileDownLoader();
			//下载网页
			downLoader.downloadFile(visitUrl);
			
			//该 url 放入到已访问的 URL 中
			LinkDB.addVisitedUrl(visitUrl);
			//提取出下载网页中的 URL
			
			Set<String> links=HtmlParserTool.extracLinks(visitUrl,filter);
			//新的未访问的 URL 入队
			for(String link:links)
			{
					LinkDB.addUnvisitedUrl(link);
			}
		}
	}
	
	/*
	 * 初始化电视节目单
	 */
	public void initUnVisitedTVUrl(){
		
		tvList = new HashMap<String,String>();
		
		//get properties
		InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("init.properties");   
		Properties p = new Properties();   
		try {   
		   p.load(inputStream);   
		} catch (IOException e1) {   
		   e1.printStackTrace();   
		}   
		String path = p.getProperty("website.src.movie");
		
		BufferedReader br;
		String str = "";
		
		try {
			br = new BufferedReader(new InputStreamReader(
					new FileInputStream(new File(path + "movie.txt"))));
			
			while((str = br.readLine()) != null){
				tvList.put(str, str);
			}
			
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} 
		
	} 

	/** 
	 * 删除文件夹下的全部文件 
	 * @param directoryPath 文件夹目录 
	 */  
	public static void deleteAllFiles(String directoryPath) {  
	    File dir = new File(directoryPath);  
	    File[] files = dir.listFiles();  
	    if (files == null) {  
	    	
	    } else {  
	        for (int i = 0; i < files.length; i++) {  
	            if (files[i].isDirectory()) {  
	            	deleteAllFiles(files[i].getAbsolutePath());  
	            } else {  
	            	files[i].delete();
	            }  
	        }  
	    }
	}


	/**
	 * @param args
	 */
	public static void main(String[] args) {
		MovieCrawler crawler = new MovieCrawler();
		crawler.movieCrawler(new String[]{"http://theater.mtime.com/China_Guangdong_Province_Shenzen/cinema/"});
		
		/*String fileStr = Class.class.getClass().getResource("/").getPath();
		Configuration rc = new Configuration(fileStr + "init.properties");
		String path = rc.getValue("website.src.movie");
		 
		List<String> filesList = TVJsoupParser.traversal(path);
		TVJsoupParser parser = new TVJsoupParser();
		for (String file : filesList){
			parser.parseHtml(file);
		}*/

		
	}

}
